package example.com.terry.servlet;

import example.com.terry.bean.PropertyDto;
import example.com.terry.utils.CookieUtils;
import example.com.terry.utils.JDBCUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.ResultSet;
import java.util.ArrayList;

/**
 * ClassName: QueryWorkServlet <br/>
 * Description: 资产查看列表 <br/>
 * Date: 2020/5/21 下午5:27 <br/>
 * <br/>
 *
 * @author ls
 * <p/>
 * 修改记录
 * @version 1.0<br />
 */
@WebServlet("/queryPropertyServlet")
public class QueryPropertyServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        boolean sign = CookieUtils.isSign(req);
        if (sign) {//账目管理
            try {
                String id = req.getParameter("id");

                if (id != null) {
                    String sql = "select * from tb_property where id = " + id;
                    ResultSet resultSet = JDBCUtils.initQuery(sql);

                    ArrayList<PropertyDto> propertyDtos = new ArrayList<>();
                    // 循环取数据库出查询出来的每条信息结果
                    if (resultSet.next()) {
                        //取出当前这条信息的字段信息
                        PropertyDto propertyDto = new PropertyDto();
                        propertyDto.setId(resultSet.getInt("id"));
                        propertyDto.setCause(resultSet.getString("cause"));
                        propertyDto.setCreateTime(resultSet.getString("create_time"));
                        propertyDto.setPrice(resultSet.getDouble("price"));
                        propertyDto.setStatus("0".equals(resultSet.getString("status")) ? "支出" : "收入");
                        propertyDto.setUpdateUser(resultSet.getString("update_user"));
                        resp.getWriter().println(propertyDto);
                    }
                } else {
                    String sql = "select * from tb_property";
                    ResultSet resultSet = JDBCUtils.initQuery(sql);

                    ArrayList<PropertyDto> propertyDtos = new ArrayList<>();
                    // 循环取数据库出查询出来的每条信息结果
                    while (resultSet.next()) {
                        //取出当前这条信息的字段信息
                        PropertyDto propertyDto = new PropertyDto();
                        propertyDto.setId(resultSet.getInt("id"));
                        propertyDto.setCause(resultSet.getString("cause"));
                        propertyDto.setCreateTime(resultSet.getString("create_time"));
                        propertyDto.setPrice(resultSet.getDouble("price"));
                        propertyDto.setStatus("0".equals(resultSet.getString("status")) ? "支出" : "收入");
                        propertyDto.setUpdateUser(resultSet.getString("update_user"));
                        propertyDtos.add(propertyDto);
                    }
                    resp.getWriter().println(propertyDtos);
                }
            } catch (Exception e) {

            }

        } else {
            resp.getWriter().println("当前非登陆状态请重新登陆");
            req.getRequestDispatcher("/login.jsp").forward(req, resp);
        }

    }
}
